﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Configuration;
using System.IO;
using BLL.Services; // 引入共享的业务逻辑类
using FlowerShop.Core.Models; // 引入数据模型


namespace 花店管理系统
{
    public partial class Login : Form
    {
        public Login()
        {
            InitializeComponent();
        }
        public static string username = "";
        public static string t_id = "";
        public static string W_id = "";
        public static bool Bflag = false;//false是普通员工，true是管理员
        public static bool Dflag = false;//false是普通员工，true是销售部员工

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {

        }
        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {

        }
        private void BtnOk_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtName.Text) || string.IsNullOrEmpty(txtPwd.Text))
            {
                MessageBox.Show("请输入用户名或密码");
                return;
            }

            UserService userService = new UserService(); // 使用共享的业务逻辑类

            if (radioButton1.Checked)
            {
                // 验证普通用户
                int t_mima;
                if (!int.TryParse(txtPwd.Text, out t_mima))
                {
                    MessageBox.Show("密码格式错误");
                    return;
                }
                var user = userService.ValidatePeopleCredentials(txtName.Text, t_mima);
                if (user != null)
                {
                    MessageBox.Show("登录成功");
                    username = txtName.Text;
                    t_id = user.t_id.ToString();
                    Form1.Aflag = true;
                    Form1.flag = 1;
                    ClearChatHistory(int.Parse(t_id)); //清空聊天记录
                    this.Close();
                    this.DialogResult = DialogResult.OK;
                }
                else
                {
                    MessageBox.Show("用户名或密码错误");
                }
            }
            else if (radioButton2.Checked)
            {
                // 验证员工
                int phone;
                if (!int.TryParse(txtPwd.Text, out phone))
                {
                    MessageBox.Show("密码格式错误");
                    return;
                }
                var worker = userService.ValidateWorkerCredentials(txtName.Text, phone);
                if (worker != null)
                {
                    MessageBox.Show("登录成功");
                    username = txtName.Text;
                    W_id = worker.W_id.ToString();
                    Form1.flag = 2;
                    Form1.Aflag = false;
                    if (worker.department == 2)
                    {
                        Dflag = true;
                    }
                    else
                    {
                        Dflag = false;
                    }
                    Tindex tl = new Tindex();
                    tl.ShowDialog();
                }
                else
                {
                    MessageBox.Show("用户名或密码错误");
                }
            }
            else
            {
                // 验证其他用户类型
                int pwd;
                if (!int.TryParse(txtPwd.Text, out pwd))
                {
                    MessageBox.Show("密码格式错误");
                    return;
                }
                var user = userService.ValidateUserCredentials(txtName.Text, pwd);
                if (user != null)
                {
                    MessageBox.Show("登录成功");
                    username = txtName.Text;
                    Form1.flag = 2;
                    this.Close();
                    Tindex tl = new Tindex();
                    tl.ShowDialog();
                }
                else
                {
                    MessageBox.Show("用户名或密码错误");
                }
            }
        }
        private void ClearChatHistory(int t_id)
        {
            // 获取连接字符串 - 尝试从配置中获取正确的连接字符串
            string connectionString = ConfigurationManager.ConnectionStrings["Model1"]?.ConnectionString;

            // 如果第一个连接字符串不可用，尝试第二个
            if (string.IsNullOrEmpty(connectionString))
            {
                connectionString = ConfigurationManager.ConnectionStrings["Model11"]?.ConnectionString;
            }

            if (string.IsNullOrEmpty(connectionString))
            {
                // 添加错误处理
                MessageBox.Show("数据库连接配置错误，请检查配置文件");
                return;
            }

            try
            {
                using (SqlConnection connection = new SqlConnection(connectionString))
                {
                    connection.Open();
                    string query = "DELETE FROM Messages WHERE UserID = @UserID";
                    using (SqlCommand command = new SqlCommand(query, connection))
                    {
                        command.Parameters.AddWithValue("@UserID", t_id);
                        command.ExecuteNonQuery();
                    }
                }
            }
            catch (Exception ex)
            {
                // 添加异常处理
                MessageBox.Show($"清除聊天记录时出错: {ex.Message}");
            }
        }
        public static string GetUserImagePath(string username)
        {
            // 获取连接字符串 - 使用与上面相同的逻辑
            string connectionString = ConfigurationManager.ConnectionStrings["Model1"]?.ConnectionString;
            if (string.IsNullOrEmpty(connectionString))
            {
                connectionString = ConfigurationManager.ConnectionStrings["Model11"]?.ConnectionString;
            }

            if (string.IsNullOrEmpty(connectionString))
            {
                return null;
            }

            string imagePath = null;

            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                connection.Open();
                string query = "SELECT Image FROM Tpeople WHERE t_name = @Username";
                using (SqlCommand command = new SqlCommand(query, connection))
                {
                    command.Parameters.AddWithValue("@Username", username);
                    SqlDataReader reader = command.ExecuteReader();

                    if (reader.Read())
                    {
                        // 获取图片路径
                        imagePath = reader["Image"].ToString();
                    }
                }
            }

            // 确保路径是安全和有效的，此处简单返回路径，实际应用中可能需要更多验证
            return imagePath;
        }
        private void label1_Click(object sender, EventArgs e)
    {

     }

    }
   

}

